<template>
  <div class="login-box">
    <div class="login-left"></div>
    <div class="login-right">
      <el-form ref="form" :model="form" :rules="rules">
        <h3 class="form-title">登录后台</h3>
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入管理员账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入账户密码"></el-input>
        </el-form-item>
        <el-form-item prop="imageCaptcha">
          <el-input v-model="form.imageCaptcha" placeholder="请输入图形验证码"></el-input>
          <img class="code" :src="codeImg" alt="" @click.stop="getCode" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loginHandle">立即登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginView',
  data() {
    return {
      // 图形码图片
      codeImg: '',
      // 图片码的key
      imagKey: '',
      // form表单数据
      form: {
        username: 'test@qq.com',
        password: 'Test123456789',
        imageCaptcha: '',
      },
      // 表单验证
      rules: {
        username: [{ required: true, message: '请您输入管理员账号', trigger: 'blur' }],
        password: [{ required: true, message: '请您输入账户密码', trigger: 'blur' }],
        imageCaptcha: [{ required: true, message: '请您输入图形验证码', trigger: 'blur' }],
      },
    };
  },
  created() {
    // 获取验证码图片
    this.getCode();
  },
  methods: {
    // 获取验证码图片
    async getCode() {
      const res = await this.$request('/captcha/image');
      if (res.status === 0) {
        console.log(res.data);
        this.codeImg = res.data.img;
        this.imageKey = res.data.key;
      }
    },
    // 处理登录请求
    loginHandle() {
      console.log(this.form.imageCaptcha);
      console.log(this.imageKey);
      // 表单预验证
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        // 登录请求
        const res = await this.$request.post('/login', {
          username: this.form.username,
          password: this.form.password,
          image_captcha: this.form.imageCaptcha,
          image_key: this.imageKey,
        });
        if (res.status === 0) {
          localStorage.setItem('meedu-admin-token', res.data.token);
          this.$message({ message: '登录成功！', type: 'success' });
          setTimeout(() => {
            this.$router.push({ name: 'DashboardView' });
          }, 1000);
        } else {
          this.getCode();
          this.$message.error(res.message);
        }
        console.log(res);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-box {
  display: flex;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 1000px;
  height: 600px;
  margin: auto;
  background: #fff;
  overflow: hidden;
  border-radius: 4px;
  box-shadow: 20px 20px 100px 0 rgb(85 102 119 / 10%);
  .login-left {
    width: 471px;
    height: 100%;
    background: url('@/assets/img/loginBg.png') no-repeat;
    background-size: 100% 100%;
    box-sizing: border-box;
  }
  .login-right {
    flex: 1;
    padding: 0px 40px 40px;
    .form-title {
      width: auto;
      height: 24px;
      margin-top: 65px;
      margin-bottom: 80px;
      font-size: 24px;
      font-weight: 500;
      color: #000;
      line-height: 24px;
    }
    /deep/ .el-button--primary {
      width: 100%;
      height: 50px;
      margin-top: 10px;
      font-size: 16px;
    }
    .el-form-item {
      margin-bottom: 50px;
    }
    /deep/ .el-input__inner {
      height: 48px !important;
    }
    /deep/ .el-form-item__content {
      display: flex;
      .code {
        display: block;
        margin-left: 20px;
      }
    }
  }
}
</style>
